//注册
/*
接口地址：http://jx.xuzhixiang.top/ap/api/reg.php
    接口请求方式：get
    接口参数：
         username用户名
         password密码
    使用方式
         http://jx.xuzhixiang.top/ap/api/reg.php?username=11&password=1212

    服务器返回json数据
*/
let btn = document.querySelector("#btn")
let ipts = document.querySelectorAll(".rgeIpt")
console.log(ipts)
let rgeIpt = document.querySelector(".regItP1")
let rgeIpt2 = document.querySelector(".regItP2")
let regUrl = `http://jx.xuzhixiang.top/ap/api/reg.php`
let decUrl = `http://jx.xuzhixiang.top/ap/api/checkname.php`
/*
检测用户名是否存在
http://jx.xuzhixiang.top/ap/api/checkname.php?username=1
*/
//判断用户名
ipts[0].onchange = function () {
  console.log("111")
  let username = this.value
  let usernameReg = /^[a-z]\w{2,16}$/i
  if (usernameReg.test(username)) {
    let p1 = axios.get(decUrl, { params: { username } })
    p1.then(function (r) {
      console.log(r)
      if ((r.data.code = 1)) {
        rgeIpt.style.display = "block"
        rgeIpt.style.color = "green"
        rgeIpt.innerHTML = r.data.msg
        ipts[0].classList.add("activeTus")
        ipts[0].classList.remove("activeFas")
      } else {
        // ipts[0].onfocus = function () {
        //   rgeIpt.style.display = "none"
        // }
        // rgeIpt.style.display = "none"
      }
    })
  } else {
    ipts[0].classList.remove("activeTus")
    ipts[0].classList.add("activeFas")
    rgeIpt.innerHTML = "用户账号格式不正确，字母开头3到16位"
    rgeIpt.style.display = "bloc k"
    rgeIpt.style.color = "red"
  }
}

//判断密码框
ipts[1].onchange = function () {
  let passwordReg = /^[a-zA-Z]\w{5,18}$/
  let password = ipts[1].value
  if (passwordReg.test(password)) {
    ipts[1].classList.add("activeTus")
    ipts[1].classList.remove("activeFas")
    rgeIpt2.innerHTML = "格式正确"
    rgeIpt2.style.color = "green"
    rgeIpt2.style.display = "block"
  } else {
    ipts[1].classList.remove("activeTus")
    ipts[1].classList.add("activeFas")
    rgeIpt2.style.color = "red"
    rgeIpt2.style.display = "block"
    rgeIpt2.innerHTML = "以字母开头，长度在6~18之间"
  }
}
// ipts[1].onfocus = function () {
//   rgeIpt2.style.display = "none"
// }

let consentCheck = document.querySelector(".consentCheck")
console.log(consentCheck.checked)
consentCheck.onclick = function () {
  let btn = document.querySelector("#btn")
  console.log(consentCheck.checked)
  if (consentCheck.checked) {
    btn.disabled = false
  } else {
    btn.disabled = true
  }
}

btn.onclick = function () {
  let username = ipts[0].value
  let password = ipts[1].value
  // let axios = require("axios")
  if (username != "" && password != "") {
    let p = axios.get(regUrl, { params: { username, password } })
    p.then(function (r) {
      console.log(r)
      if (r.data.code == 1) {
        alert("注册成功")
        location.href = "logIn.html"
      } else {
        alert("注册失败")
      }
    })
  } else {
    alert("请输入账号")
  }
}
